<?php

global $CFG;

require_once($CFG->libdir. '/simpletestlib/unit_tester.php');
require_once($CFG->libdir. '/simpletestlib/mock_objects.php');

require_once(dirname(__FILE__).'/../tracker_database.php');

class tracker_database_test extends UnitTestCase {
    
    function get_items_as_string($items) {
        $output='';
        foreach($items as $item) {
            $output.=$this->get_item_as_string($item)."\n";
        }
        return $output;
    }
    
    function get_item_as_string($item) {
        return $item->get_public_code()."\t".$item->get_edition_num()."\t".$item->get_use_date()."\t".
            $item->get_first_use_pres()."\t".$item->is_for_students()."\t".$item->is_for_tutors()."\t".
            $item->get_extended_public_code()."\t".$item->get_title();
    }
    
    function test_valid() {
        // Note that these tests will fail if any data referring to the 2004 or 04J S103 course changes
        // in PLANET. This might indicate a genuine change rather than a failure of the code, but is
        // (we judge) fairly unlikely to occur.
        $tracker=new tracker_database();
        $items=$tracker->get_items('S103','2004');
        $this->assertEqual(count($items),59);
        $items=$tracker->get_items('s103','04j');
        $this->assertEqual(count($items),38);
        
        $contents=$this->get_items_as_string($items);
        // Uncomment this line to save the contents when updating the test
//        file_put_contents(dirname(__FILE__).'/../../../../../../moodledata/tracker.testcase',$contents);
        $storedcontents=file_get_contents(dirname(__FILE__).'/tracker.testcase');        
        $this->assertEqual($contents,$storedcontents);
        
        // Second test (contains some tutor-only items)
        $items=$tracker->get_items('d845','04e');        
        $contents=$this->get_items_as_string($items);
        // Uncomment this line to save the contents when updating the test
//        file_put_contents(dirname(__FILE__).'/../../../../../../moodledata/tracker2.testcase',$contents);
        $storedcontents=file_get_contents(dirname(__FILE__).'/tracker2.testcase');        
        $this->assertEqual($contents,$storedcontents);        
     }
     
     function test_invalid() {
        $tracker=new tracker_database();
        $items=$tracker->get_items('s103','04e');
        $this->assertEqual(count($items),0);
        $items=$tracker->get_items('z666','06');
        $this->assertEqual(count($items),0);
        $items=$tracker->get_items('dedoodedoodedoo','hoandthenagainhum');
        $this->assertEqual(count($items),0);        
     }            
}
?>
